/**
 * name: kent
 */
angular.module('kt.httpUrlInterceptor', [])
    .provider('httpUrlInterceptor', function () {


        this.contextPath = "/";
        this.setContextPath = function(contextPath){
            if(contextPath){
                //如果不是以'/'结尾，添加上字符'/'
                if(contextPath.charAt(contextPath.length-1) != '/'){
                    contextPath += '/';
                }
                this.contextPath = contextPath;
            }
        };

        var transform = function (data) {
            return $.param(data);
        };

        this.$get = function(){
            var self = this;
            return {
                request: function(config){
                    //url前添加特定的部署路径
                    config.url = self.contextPath + config.url;

                    console.log(config.url);

                    //处理post请求传参数,防止后台取不到
                    if (config.method == "POST"){
                        config.headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'};
                        config.transformRequest = transform;
                    }

                    return config;
                }
            }
        };
    });
